#!/usr/bin/env python """ Example of a simple embedded engine in a wxpython application. """ import wx from ptk_lib.engine import wx_engine, eng_misc class TestApp(wx.App): def __init__(self): wx.App.__init__(self) self.frame = wx.Frame(None, -1, 'test') self.frame.Show() #create a locals dictionary for the engine this contains objects that #appear in the top level user namespace accessed via the controlling PTK #console. #use the __main__ namespace (this file) as the top #level. import __main__ usr_dict = __main__.__dict__ #Alternatively this could be an empty dictionary in which you #can store objects you want to expose to the engine. usr_dict = {} usr_dict['a'] = 1 #expose objects to the engine. usr_dict['b'] = 'test string' #create the engine object # parent - the parent wxpython object used for events # engine label - The label display to the user on the console tab in # PTK # userdict - the local dictionary for the top level namespace self.eng = wx_engine.wxEngine( self, 'wxTestApp', userdict=usr_dict) #bind to the engine close events self.Bind(wx_engine.EVT_ENGINE_DISCONNECT, self.OnEngineDisconnect) #make sure we close the engine when exiting. self.frame.Bind(wx.EVT_CLOSE, self.OnClose) #connect to a running PTK instance port = eng_misc.get_message_port() self.eng.connect( 'localhost', port) def Exit(self): self.eng.disconnect() wx.App.Exit(self) def OnClose(self,event): self.eng.disconnect() event.Skip() def OnEngineDisconnect(self, event): print 'Engine disconnected' app = TestApp() app.MainLoop()